<template>
  <div class="detail-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账号设置" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="350px">
            <el-form-item label="用户名:" prop="username">
              <el-input v-model="form.username" style="width:30%" />
            </el-form-item>
            <el-form-item label="密码:" prop="password">
              <el-input
                v-model="form.password"
                type="password"
                style="width:30%"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">更新</el-button>
              <el-button @click="cancelHandler">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <Info />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <Job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getMoreInfo } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import Info from './components/Info.vue'
import Job from './components/Job.vue'

export default {
  name: 'Detail',
  components: {
    Info,
    Job
  },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      userInfo: {},
      rules: {
        username: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 9,
            message: '密码长度为6-9个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  async created() {
    const res = await getMoreInfo({ id: this.$route.params.id })
    // console.log(res)
    this.userInfo = res
    this.form.username = res.username
  },
  methods: {
    cancelHandler() {
      this.$router.back()
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          await saveUserDetailById({ ...this.userInfo, ...this.form })
          this.$message.success('修改成功！')
          this.$router.back()
        } else {
          this.$message.error('数据格式有误，请检查！')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.detail-container {
  padding: 20px;
}
</style>
